<?php
namespace app\common\model;

class User extends Base
{
    protected $name = 'user';
    protected $updateTime = false;

    public function getAvatarAttr($val){
        if(!$val){
            return '';
        }

        if(stristr($val, 'storage') != false){
            return config('app.url') . $val;
        }else{
            return $val;
        }
    }

    public function getStarAttr($val){
        if(!$val){
            return '普通会员';
        }

        return (new Star())->getVal(['grade' => $val], 'name')['data'];
    }

    /** 更新会员等级 */
    public function reckonGrade($user_id, $num = 0)
    {
        if(!$num || !$user_id){
            return;
        }
        
        /** 更新当前进货数量 */
        $this->setIncField(['id' => $user_id], 'buy_number', $num);

        /** 查询用户信息 */
        $userInfo = $this->where('id', $user_id)->find();

        $starModel = new Star();

        /** 查询星级列表 */
        $starList = $starModel->where([['grade', '>', $userInfo->getData('star')]])->order('grade')->select();

        if(!$starList){
            return;
        }

        /** 当前进货数量 */
        $buyNum = $userInfo['buy_number'];

        foreach($starList as $v){
            if($buyNum < $v['number']){
                continue;
            }

            /** 更新会员等级 */
            $this->setIncField(['id' => $user_id], 'star');

            /** 更新会员折扣 */
            $this->update(['id' => $user_id, 'discount' => $v['discount']]);

            /** 更新进货总量 */
            $this->setIncField(['id' => $user_id], 'buy_number_all', $v['number']);

            /** 减去升级所需当前进货数量 */
            $this->setDecField(['id' => $user_id], 'buy_number', $v['number']);

            $buyNum -= $v['number'];
        }
    }

    /** 发放推荐奖 */
    public function pushAward($user_id, $money = 0, $order_id = 0)
    {
        /** 用户信息 */
        $userInfo = $this->where('id', $user_id)->find();

        if(!$userInfo['pid']){
            return ;
        }

        /** 用户直接推荐人信息 */
        $userSecInfo = $this->where('id', $userInfo['pid'])->find();

        if(!$userSecInfo){
            return ;
        }

        /** 直接推荐奖励 */
        $pushFstAward = 0;

        /** 间接推荐奖励 */
        $pushSecAward = 0;

        $systemModel = new System();

        if($userInfo['star'] == $userSecInfo['star']){
            $pushFstAward = $systemModel->getVal(['id' => 1], 'push_award_fst')['data'];
        }else if($userInfo['star'] < $userSecInfo['star']){
            $pushFstAward = ($userInfo['discount'] - $userSecInfo['discount']) * $money;
        }

        if($pushFstAward > 0){
            /** 增加佣金 */
            $this->setIncField(['id' => $userSecInfo['id']], 'money', $pushFstAward);

            /** 写入财务记录 */
            addFin($userSecInfo['id'], $pushFstAward, '推荐奖(直接)', $order_id, 1);
        }

        if(!$userSecInfo['pid']){
            return ;
        }

        /** 用户间接推荐人信息 */
        $userThrInfo = $this->where('id', $userSecInfo['pid'])->find();

        if(!$userThrInfo){
            return ;
        }

        if($userInfo['star'] == $userThrInfo['star']){
            $pushSecAward = $systemModel->getVal(['id' => 1], 'push_award_sec')['data'];
        }else if($userInfo['star'] < $userThrInfo['star']){
            $pushSecAward = ($userInfo['discount'] - $userThrInfo['discount']) * $money;
        }

        if($pushSecAward > 0){
            /** 增加佣金 */
            $this->setIncField(['id' => $userThrInfo['id']], 'money', $pushSecAward);

            /** 写入财务记录 */
            addFin($userThrInfo['id'], $pushSecAward, '推荐奖(间接)', $order_id, 1);
        }
    }

    /** 检测并发放团队奖 */
    public function teamAward($user_id = 0)
    {
        if(!$user_id){
            return ;
        }

        $userInfo = $this->where('id', $user_id)->find();

        /** 查询直推人数 */
        $pushNum = $this->where('pid', $user_id)->count();

        /** 查询用户团队总进货量 */
        $teamSalesNum = $this->getSales([$user_id]);

        $teamRewardRuleModel = new TeamRewardRule();

        /** 查询下一等级团队奖励 */
        $teamInfo = $teamRewardRuleModel->where([['id', '>', $userInfo['team_reward']]])->limit(1)->order('id')->find();

        if(!$teamInfo){
            return false;
        }

        if($teamInfo['push_number'] <= $pushNum && $teamInfo['sales'] <= $teamSalesNum){
            /** 更新会员团队奖标记 */
            $this->setIncField(['id' => $user_id], 'team_reward');

            /** 增加佣金 */
            $this->setIncField(['id' => $user_id], 'money', $teamInfo['reward']);

            /** 写入财务记录 */
            addFin($user_id, $teamInfo['reward'], '团队奖');
        }
    }

    /** 查询用户团队总进货量 */
    public function getSales($ids, &$salesNum = 0)
    {
        $salesNum += $this->where([['id', 'in', $ids]])->sum('buy_number');

        $userIds = $this->where([['pid', 'in', $ids]])->column('id');

        if($userIds){
            $this->getSales($userIds, $salesNum);
        }

        return $salesNum;
    }

    /** 检测并发放业绩奖 */
    public function achievAward($user_id = 0)
    {
        if(!$user_id){
            return ;
        }

        $userInfo = $this->where('id', $user_id)->find();

        $orderModel = new Order();

        /** 查询用户团队年进货量 */
        $teamYearSalesNum = $this->getYearSales([$user_id], $orderModel);

        $achievModel = new Achiev();

        /** 查询下一等级业绩奖励 */
        $achievInfo = $achievModel->where([['id', '>', $userInfo['achiev_reward']]])->limit(1)->order('id')->find();

        if(!$achievInfo){
            return false;
        }

        if($achievInfo['sales'] <= $teamYearSalesNum){
            /** 更新会员业绩奖标记 */
            $this->setIncField(['id' => $user_id], 'achiev_reward');

            /** 增加佣金 */
            $this->setIncField(['id' => $user_id], 'money', $achievInfo['reward']);

            /** 写入财务记录 */
            addFin($user_id, $achievInfo['reward'], '业绩奖');
        }
    }

    /** 查询用户团队年进货量 */
    public function getYearSales($ids, $orderModel, &$salesNum = 0)
    {
        $salesNum += $orderModel->where([['user_id', 'in', $ids], ['status', 'in', [2,3,4]]])->whereYear('create_time')->sum('total');

        $userIds = $this->where([['pid', 'in', $ids]])->column('id');

        if($userIds){
            $this->getYearSales($userIds, $orderModel, $salesNum);
        }

        return $salesNum;
    }
}